<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cancelsales_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		


	public function insert_cancel($data){
		
	//print_r($data);exit;
	$account=$this->db->delete("accounts",array("id_user" => $data['id_customer'],"voucher_no" => $data['invoice_no'] ));
	//print_r($account);exit;
	$this->db->insert('cancel_sales',$data);
	$latest_id = $this->db->insert_id();
	$up_data = array("cancel_status" => 1);
	$this->db->update("sales",$up_data,array('id' => $data['id_sales']));
	return $latest_id; 		
	}

	public function insert_sales($data){		
	
	if($data['transaction_type']== 'Cash'){
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			/*if($data['customer_name']!=''){
				$data['customer_name']= ' ';	
			}
			*/$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => 'Cash',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'credit_amount' => $data['total_price'],
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}else{
			$data['transaction_type']="Credit";
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => '',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}
			return $latest_id;
	
	$this->db->insert('sales',$data);
	$latest_id = $this->db->insert_id();
	return $latest_id; 
		
	}	

	
	public function insert_cancelproduct($cancel_data, $sales_data,$id_branch ){
	
		$product =  $this->db->get_where("product",array( "id" => $cancel_data['id_product'] ))->row_array();
		$product_stock = intval($product['opening_stock']) + intval($cancel_data['cancel_quantity']);
		$up_product = array("opening_stock" => $product_stock );	
		$branch =  $this->db->get_where("branch_product",array("id_branch" => $id_branch,  "id_product" => $cancel_data['id_product'] ))->row_array();
		$new_stock = intval($branch['opening_stock']) + intval($cancel_data['cancel_quantity']);
		$up_branch = array("opening_stock" => $new_stock);
		$this->db->insert('cancelsales_product',$cancel_data);
		$this->db->insert('sales_product',$sales_data);
		$this->db->update("branch_product",$up_branch,array("id_branch" => $id_branch , "id_product" => $cancel_data['id_product']));
		$this->db->update("product",$up_product,array("id" => $cancel_data['id_product']));
		
	}




	
public function get(){
	   $this->datatables
		->select('id,invoice_no,new_invoice_no,billed_date,date,customer_name , previous_total,cancel_total,new_total')
		->from('cancel_sales' )
		
		/*	
			->select('product.id as id ,product.name,product.code, category.name AS category_name, company.name AS company_name,product.purchase_price,product.selling_price,product.opening_stock,product.alert_quantity')
			->from('product', 'company', 'category')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')*/
			->add_column("Actions", 
			"<center>			<a title='PRINT' href='report/$1'>
								<i class='icon-print'></i>
								</a>
							   </center>", "id")
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}



public function getcancel_sales($id){
	
	$query = $this->db->get_where('cancel_sales', array('id' => $id));
	return $query;
}

public function get_uniquesale($id){
	$query = $this->db->get_where('cancelsales_product', array('id_cancel' => $id));
	return $query;
	
}


}
